package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.FishPondSpecialItemData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/10/14 15:31
 */
public class UserFishPondSpecialItemDataDaoImpl extends TempDataObjectDao<FishPondSpecialItemData> {
    private static String insertSql = "insert into t_u_fishpond_specialitem(userId,specialItemId,count,isReadSample,isGetUnLockReward) " +
            "values(?,?,?,?,?)";
    @Override
    protected boolean addData(FishPondSpecialItemData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSpecialItemId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCount()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReadSample()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnLockReward()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    private static String updateSql = "update t_u_fishpond_specialitem set count=?,isReadSample=?,isGetUnLockReward=? where userId=? and specialItemId=?";
    @Override
    protected boolean updateData(FishPondSpecialItemData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getCount()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReadSample()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnLockReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSpecialItemId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected FishPondSpecialItemData parseData(ResultSet rs) throws SQLException {
        FishPondSpecialItemData data = new FishPondSpecialItemData();
        data.setUserId(rs.getLong("userId"));
        data.setSpecialItemId(rs.getInt("specialItemId"));
        data.setCount(rs.getLong("count"));
        data.setReadSample(rs.getBoolean("isReadSample"));
        data.setGetUnLockReward(rs.getBoolean("isGetUnLockReward"));
        return data;
    }

    private static String getSql = "select * from t_u_fishpond_specialitem where userId = ?";

    public Map<Integer, FishPondSpecialItemData> getFishPondSpecialItemDataMap(long playerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, FishPondSpecialItemData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, playerId);
            rs = call.executeQuery();
            while (rs.next()) {
                FishPondSpecialItemData data = parse(rs);
                map.put(data.getSpecialItemId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
